/**
 * 
 */
package com.estock.platform.http.response;

import java.util.Arrays;

import org.apache.commons.lang.StringUtils;

import com.estock.common.util.StringUtil;
import com.estock.platform.http.utils.RequestUtil;

/**
 * need to enhance to improve performance in parse and convert methods
 * 
 * @author richard
 *
 */
public abstract class SinaBaseResponse extends BaseResponse {
	
	@Override
	protected void setBody(String body) throws Exception {
		this.body = RequestUtil.decode(body, RequestUtil.CHARSET_GBK);
		if (!StringUtils.isEmpty(body)) {
			// Returned string is not json format, it's something like
			// var hq_str_s_sh000010="上证180,10024.26,237.633,2.43,1777574,23076873";
			// var hq_str_sz000625="长安汽车,0.00,20.29,0.00,0.00,0,0.00,0.00,0,0.00,0,0.00,0,0.00,0,0.00,0,0.00,0,0.00,0,0.00,0,0.00,0,0.00,0,0.00,0,0.00,2015-03-20,13:42:22,03";
			String[] hqstrs = body.split(";");
			for (String sinaJsString : Arrays.asList(hqstrs)) {
				if (StringUtil.isEmpty(sinaJsString) ||
						StringUtils.equals(sinaJsString, "\n")) continue;
				String indexJsBody = sinaJsString.split("=")[1];
				if (StringUtil.isEmpty(indexJsBody) ||
						StringUtils.equals(indexJsBody, "\"\"")) continue;
				parse(sinaJsString);
			}
			
		}
	}
	
	protected abstract void parse(String sinaJsString);

}
